package vip.xiaonuo.smzq.modular.jsxm.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb01Zh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb01ZhMapper;
import vip.xiaonuo.smzq.modular.jsxm.entity.PsbaYqclBscl;
import vip.xiaonuo.smzq.modular.jsxm.entity.vo.Dzbj;
import vip.xiaonuo.smzq.modular.jsxm.entity.vo.JsxmfjVo;
import vip.xiaonuo.smzq.modular.jsxm.entity.vo.Sbcl;
import vip.xiaonuo.smzq.modular.jsxm.mapper.PsbaYqclBsclMapper;
import vip.xiaonuo.smzq.modular.jsxm.service.IPsbaYqclBsclService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 建设项目—附件 服务实现类
 *
 * @author sqq
 * @since 2024-05-24
 */
@Service
public class PsbaYqclBsclServiceImpl extends ServiceImpl<PsbaYqclBsclMapper, PsbaYqclBscl> implements IPsbaYqclBsclService {

    @Resource
    private PsbaYqclBsclMapper psbaYqclBsclMapper;
    @Resource
    private Cb01ZhMapper cb01ZhMapper;

    @Override
    public Page<PsbaYqclBscl> page(PsbaYqclBscl psbaYqclBscl) {
        QueryWrapper<PsbaYqclBscl> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public JsxmfjVo getJsxmfjVo(String jsxmbh) {
//        try {
            JsxmfjVo jsxmfjVo = new JsxmfjVo();
            jsxmfjVo.setPsbabh(jsxmbh);
            List<Sbcl> sbclList = new ArrayList<>();
            QueryWrapper<Cb01Zh> productionStatu = new QueryWrapper<>();
            productionStatu.eq("bh", 307);
            productionStatu.ne("bnm", 0);
            List<Cb01Zh> fjlxList = cb01ZhMapper.selectList(productionStatu);
            if (fjlxList != null) {
                for (Cb01Zh cb01Zh : fjlxList) {
                    Sbcl sbcl = new Sbcl();
                    sbcl.setClbh(cb01Zh.getBnm());
                    sbcl.setClmc(cb01Zh.getMc());
                    QueryWrapper<PsbaYqclBscl> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("psbabh", jsxmbh);
                    queryWrapper.eq("fjlx", cb01Zh.getMc());
                    List<PsbaYqclBscl> psbaYqclBsclList = psbaYqclBsclMapper.selectList(queryWrapper);
                    if (psbaYqclBsclList != null) {
                        sbcl.setClsl(psbaYqclBsclList.size());
                        List<Dzbj> dzbjList=new ArrayList<>();
                        for (PsbaYqclBscl psbaYqclBscl:psbaYqclBsclList){
                            Dzbj dzbj=new Dzbj();
                            dzbj.setBjmc(psbaYqclBscl.getWjmc());
                            dzbj.setBjlj(psbaYqclBscl.getWjlj());
                            //文件大小还未找到对应字段 先设0
                            dzbj.setBjdx(0);
                            dzbjList.add(dzbj);
                        }
                        sbcl.setDzbjList(dzbjList);
                    }
                    sbclList.add(sbcl);
                }
            }
            jsxmfjVo.setSbclList(sbclList);

            return jsxmfjVo;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


       // return null;
    }
}
